package com.yanqu.road.server.logic.thread.client.msg.push.task;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.enums.client.msg.push.ePushType;
import com.yanqu.road.entity.push.ClientPushRecord;
import com.yanqu.road.server.helper.SignHelper;
import com.yanqu.road.server.manger.AppKeyMgr;
import com.yanqu.road.server.manger.sdk.ClientMsgPushMgr;
import com.yanqu.road.utils.MD5Security;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @Author wpj
 * @Date 2021/3/21
 */
public class PushClientMsgTask extends GeneralTask {

    //标题
    private String title;
    //正文
    private String text;
    // 目标 single 单个 list一批 all 所有玩家
    private String target;
    // single 必填  list 必填账号,隔开 一次最多200个uid 应该是平台uid
    private String audience;
    // 动作 startapp 唤起app  url 打开网址 intent 打开应用内特定页面
    private String clickType;
    // clickType url时必填
    private String url;
    // 页面必填
    private String intent;
    // 必填
    private String extension;
    // appid
    private String appId;
    // 子游戏id
    private String gameId;

    // type
    private int clientMsgType;

    public PushClientMsgTask(String title, String text, String target, String audience, String clickType, String url, String intent, String appId, String gameId, int clientMsgType) {
        this.title = title;
        this.text = text;
        this.target = target;
        this.audience = audience;
        this.clickType = clickType;
        this.url = url;
        this.intent = intent;
        this.appId = appId;
        this.gameId = gameId;
        this.clientMsgType = clientMsgType;
    }

    @Override
    public void doRun() {

        JSONObject postData = new JSONObject();
        postData.put("title", title);
        postData.put("text", text);
        postData.put("target", target);
        postData.put("audience", audience);
        postData.put("click_type", clickType);
        postData.put("url", url);
        postData.put("intent", intent);
        JSONObject extension = new JSONObject();
        extension.put("appid", 690);
        extension.put("gameid", gameId);
        postData.put("extension", extension.toJSONString());

        String sign = null;
        try {
            sign = MD5Security.md5(AppKeyMgr.getAndroidAppId() + AppKeyMgr.getPlatformChannelId() + AppKeyMgr.getAndroidAppSecret()).toLowerCase();
        } catch (Exception e) {
            e.printStackTrace();
        }
        postData.put("sign", sign);

        try {
            String url = AppKeyMgr.getPlatformBaseUrl() + "/msg/sendAppMsg/" + AppKeyMgr.getAndroidAppId() + "/" + AppKeyMgr.getPlatformChannelId();
            getLOGGER().info("sendAppMsg url:{}", url);
            String result = HttpHelper.doPostJsonTimeOut(url, postData.toJSONString(), "UTF-8", new HashMap<>());
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getIntValue("code") == 1) {
                // 请求成功 写入次数表示已收到推送
                for (long userId : StringUtils.stringToLongList(audience, ",")) {
                    ClientPushRecord clientPushRecord = ClientMsgPushMgr.getClientPushRecord(userId, clientMsgType);
                    if (clientPushRecord != null) {
                        clientPushRecord.setTimes(clientPushRecord.getTimes() + 1);
                    }
                }
            }

        } catch (Exception e) {
            getLOGGER().error("sendAppMsg error:",e);
        }

    }
}
